/*
 * @Author: szx
 * @Date: 2021-12-15 14:41:28
 * @LastEditTime: 2021-12-15 14:42:35
 * @Description:
 * @FilePath: \leetcode\600-699\637\637.js
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var averageOfLevels = function (root) {
    const res = [];
    if (!root) return res;
    const que = [];
    que.push(root);
    while (que.length) {
        const length = que.length;
        let sum = 0;
        for (let i = 0; i < length; i++) {
            const tmp = que.shift();
            sum += tmp.val;
            if (tmp.right) que.push(tmp.right);
            if (tmp.left) que.push(tmp.left);
        }
        res.push(sum / length);
    }
    return res;
};
